﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;
namespace BLL
{
    public class ToBLL
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable SelectDes()
        {
            return DB.Tos.OrderByDescending(s => s.MaTo).AsQueryable();
        }
        public string Insert(ToDTO to)
        {
            try
            {
                To t = new To();
                t.MaTo = (id++).ToString();
                t.TenTo = to.Tento;
                DB.Tos.InsertOnSubmit(t);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Update(ToDTO to)
        {
            try
            {
                To t = DB.Tos.Where(s => s.MaTo == to.Mato).FirstOrDefault();
                t.TenTo = to.Tento;
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Delete()
        {
            try
            {
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public List<ToDTO> getTo()
        {
            //lay het du lieu tu bang khoi
            List<To> list = DB.Tos.ToList<To>();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<ToDTO> listdto = new List<ToDTO>();
            foreach (To to in list)
            {
                ToDTO k = new ToDTO();
                k.Mato= to.MaTo;
                k.Tento = to.TenTo;
                listdto.Add(k);
            }
            return listdto;
        }
        public bool check(string tento)
        {
            var kq = DB.Tos.Where(t=>t.TenTo==tento).ToList();
            int kt = 0;
            kt = kq.Count();
            if (kt == 0)
                return true;
            else return false;
        }
    }
}
